package com.example.takephotovideo.customview.headview;


import android.content.Context;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.takephotovideo.R;


/**
 * @功能: 自定义标题栏View
 * @Creat 2019/07/16 9:59
 * @User Lmy
 * @By Android Studio
 */
public class HeadCustomView extends RelativeLayout implements View.OnClickListener {
    View inflate;


    public static final int BACK_IV = 1;//点击返回按钮
    public static final int BACK_TV = 2;//点击返回文字
    public static final int TITLE = 3;//点击标题
    public static final int RIGHT_TV_01 = 4;//点击右侧第一个文字
    public static final int RIGHT_TV_02 = 5;//点击右侧第二个文字
    public static final int RIGHT_IV = 6;//点击右侧按钮
    public static final int SEARCH_IV = 7;//点击输入框中的搜索按钮
    public static final int SEARCH_ET = 8;//点击键盘的搜索按钮的回调标记

    private RelativeLayout rlAll;//最外层布局用于设置头布局的背景色
    private ImageView ivBack;//左侧图片返回按钮
    private TextView tvBack;//左侧文字返回按钮
    private TextView tvTitle;//中间标题
    private RelativeLayout rlSearch;//中间搜索布局的外层布局用于控制显示隐藏和背景色设置
    private ImageView ivSearch;//中间搜索布局的搜索图标按钮用于重新设置搜索图标
    private EditText etSearch;//中间搜索输入框控件
    private TextView tvRight01;//右侧第一个文字按钮
    private ImageView ivRight;//右侧图片按钮
    private TextView tvRight02;//右侧第二个文字按钮


    private OnClickHeadView clickCallback;//头部局的点击事件的回调


    public HeadCustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        inflate = LayoutInflater.from(context).inflate(R.layout.title_layout, this, true);


        rlAll = (RelativeLayout) inflate.findViewById(R.id.rl_all);
        ivBack = (ImageView) inflate.findViewById(R.id.iv_back);
        tvBack = (TextView) inflate.findViewById(R.id.tv_back);
        tvTitle = (TextView) inflate.findViewById(R.id.tv_title);
        rlSearch = (RelativeLayout) inflate.findViewById(R.id.rl_search);
        ivSearch = (ImageView) inflate.findViewById(R.id.iv_search);
        etSearch = (EditText) inflate.findViewById(R.id.et_search);
        tvRight01 = (TextView) inflate.findViewById(R.id.tv_right_01);
        ivRight = (ImageView) inflate.findViewById(R.id.iv_right);
        tvRight02 = (TextView) inflate.findViewById(R.id.tv_right_02);
        ivBack.setOnClickListener(this);
        tvBack.setOnClickListener(this);
        tvTitle.setOnClickListener(this);
        tvRight01.setOnClickListener(this);
        ivRight.setOnClickListener(this);
        tvRight02.setOnClickListener(this);
        ivSearch.setOnClickListener(this);

        etSearch.setFocusable(true);
        etSearch.setFocusableInTouchMode(true);
        etSearch.requestFocus();
        etSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event) {
//                if (actionId == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
//                    clickCallback.onHeadViewOnClick(SEARCH_ET);
//                }
//                return false;
                if ((actionId == 0 || actionId == 3) && event != null) {
                    clickCallback.onHeadViewOnClick(SEARCH_ET);
                    hideKeyboard(inflate);
                }
                return false;
            }
        });
    }


    /**
     * 隐藏软键盘
     *
     * @param view :一般为EditText
     */
    public void hideKeyboard(View view) {
        InputMethodManager manager = (InputMethodManager) view.getContext()
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        manager.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }


    public HeadCustomView(Context context) {
        this(context, null);
    }

    /***********************************************************设置View的显示风格文字大小颜色背景等************************************************************/


    /***********************************************************设置头布局的图标资源************************************************************/

    /**
     * 设置返回键图标资源
     */
    public void setImageResourceIvBack(int resource) {
        ivBack.setImageResource(resource);
    }

    /**
     * 设置中间搜索图标的资源
     */
    public void setImageResourceIvSerach(int resource) {
        ivSearch.setImageResource(resource);
    }

    /**
     * 设置右侧imageview图标资源
     */
    public void setImageResourceIvRight(int resource) {
        ivRight.setImageResource(resource);
    }

    /**
     * 设置右侧imageview图标资源大小
     */
    public void setImageSizeIvRight(int x, int y) {
        ViewGroup.LayoutParams params = ivRight.getLayoutParams();
        params.width = x;
        params.height = y;
        ivRight.setLayoutParams(params);


    }
    /***********************************************************设置头布局的TextView和EditTextView文字颜色************************************************************/

    /**
     * 设置返回文字颜色
     *
     * @author :limingyang
     */
    public void setTextColorTvBack(int color) {
        tvBack.setTextColor(color);
    }

    /**
     * 设置中间标题文字颜色
     */
    public void setTextColorTitle(int color) {
        tvTitle.setTextColor(color);
    }

    /**
     * 设置右侧第一个Textview颜色
     */
    public void setTextColorTvRight01(int color) {
        tvRight01.setTextColor(color);
    }

    /**
     * 设置右侧第二个Textview颜色
     */
    public void setTextColorTvRight02(int color) {
        tvRight02.setTextColor(color);
    }

    /**
     * 设置搜索提示文字颜色
     */
    public void setHintColorEtSearch(int color) {
        etSearch.setHintTextColor(color);
    }

    /**
     * 设置搜索输入文字颜色
     */
    public void setTextColorEtSearch(int color) {
        etSearch.setTextColor(color);
    }

    /***********************************************************设置头布局的TextView和EditTextView文字大小************************************************************/
    /**
     * 设置返回文字大小
     */
    public void setTextSizeTvBack(int size) {
        tvBack.setTextSize((float) size);
    }

    /**
     * 设置中间标题文字大小
     */
    public void setTextSizeTitle(int size) {
        tvTitle.setTextSize((float) size);
    }

    /**
     * 设置右侧第一个Textview文字大小
     */
    public void setTextSizeTvRight01(int size) {
        tvRight01.setTextSize((float) size);
    }

    /**
     * 设置右侧第二个Textview文字大小
     */
    public void setTextSizeTvRight02(int size) {
        tvRight02.setTextSize((float) size);
    }

    /**
     * 设置搜索输入文字的大小
     */
    public void setTextSizeEtSearch(int size) {
        etSearch.setTextSize((float) size);
    }


    /***********************************************************设置头布局的TextView和EditTextView文字************************************************************/
    /**
     * 设置标题背景色
     */
    public void setBackgroundAll(int color) {
        rlAll.setBackgroundColor(color);
    }

    /**
     * 设置搜索背景色
     */
    public void setBackgroundSearch(int color) {
        rlSearch.setBackgroundColor(color);
    }

    /**
     * 设置返回文字
     */
    public void setTextTvBack(String text) {
        tvBack.setText(text);
    }

    /**
     * 设置中间标题文字
     */
    public void setTextTitle(String text) {
        tvTitle.setText(text);
    }

    /**
     * 设置右侧第一个Textview文字
     */
    public void setTextTvRight01(String text) {
        tvRight01.setText(text);
    }

    /**
     * 设置右侧第二个Textview文字
     */
    public void setTextTvRight02(String text) {
        tvRight02.setText(text);
    }

    /**
     * 设置搜索提示文字
     */
    public void setHintEtSearch(String text) {
        etSearch.setHint(text);
    }


    @Override
    public void onClick(View view) {
        if (clickCallback == null) {
            return;
        }
        int i = view.getId();
        switch (i) {
            case R.id.iv_back:
                clickCallback.onHeadViewOnClick(BACK_IV);
                break;
            case R.id.tv_back:
                clickCallback.onHeadViewOnClick(BACK_TV);
                break;
            case R.id.tv_right_01:
                clickCallback.onHeadViewOnClick(RIGHT_TV_01);
                break;
            case R.id.tv_right_02:
                clickCallback.onHeadViewOnClick(RIGHT_TV_02);
                break;
            case R.id.iv_right:
                clickCallback.onHeadViewOnClick(RIGHT_IV);
                break;
            case R.id.tv_title:
                clickCallback.onHeadViewOnClick(TITLE);
                break;
            case R.id.iv_search:
                clickCallback.onHeadViewOnClick(SEARCH_IV);
                break;
        }
    }

    /**
     * 控制View的显示隐藏
     */
    private void setViewIsShow(View view, int visibility) {
        view.setVisibility(visibility);
    }

    public String getEditText() {
        return etSearch.getText().toString();
    }

    /***********************************************************控制控件的显示隐藏方法************************************************************/

    /**
     * 隐藏返回图片按钮
     *
     * @author :limingyang
     */
    public void setIvBackGone() {
        setViewIsShow(ivBack, GONE);
    }

    /**
     * 显示返回图片按钮
     *
     * @author :limingyang
     */
    public void setIvVisible() {
        setViewIsShow(ivBack, VISIBLE);
    }


    /**
     * 隐藏返回文字按钮
     *
     * @author :limingyang
     */
    public void setTvBackGone() {
        setViewIsShow(tvBack, GONE);
    }


    /**
     * 显示返回文字按钮
     *
     * @author :limingyang
     */
    public void setTvBackVisible() {
        setViewIsShow(tvBack, VISIBLE);
    }


    /**
     * 显示头部标题按钮
     */
    public void setTvTitleVisible() {
        setViewIsShow(tvTitle, VISIBLE);
    }

    /**
     * 隐藏头部标题按钮
     */
    public void setTvTitleGone() {
        setViewIsShow(tvTitle, GONE);
    }


    /**
     * 隐藏右侧第一个文字按钮
     */
    public void setTvRight01Visible() {
        setViewIsShow(tvRight01, VISIBLE);
    }

    /**
     * 隐藏右侧第一个文字按钮
     */
    public void setTvRight01Gone() {
        setViewIsShow(tvRight01, GONE);
    }

    /**
     * 显示右侧第二个文字按钮
     */
    public void setTvRight02Visible() {
        setViewIsShow(tvRight02, VISIBLE);
    }

    public void setTvRight02Gone() {
        setViewIsShow(tvRight02, GONE);
    }

    /**
     * 隐藏右侧的图片图标
     */
    public void setIvRightGone() {
        setViewIsShow(ivRight, GONE);
    }

    /**
     * 显示右侧的图片图标
     */
    public void setIvRightVisible() {
        setViewIsShow(ivRight, VISIBLE);
    }


    /**
     * 隐藏搜索布局
     */
    public void setRlSearchGone() {

        setViewIsShow(rlSearch, GONE);
    }

    /**
     * 显示搜索布局
     */
    public void setRlSearchVisible() {
        setViewIsShow(rlSearch, VISIBLE);
    }

    /***********************************************************控制控件的显示隐藏方法************************************************************/

    /**
     * 头布局控件的点击事件的监听
     */

    public void setOnClickHeadView(OnClickHeadView clickCallBack) {
        this.clickCallback = clickCallBack;
    }

}
